/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAO.DAO_Student;
import Entity.Student;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Hieu
 */
public class BulkAdd extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String comfirm = request.getParameter("comfirm");

        HttpSession session = request.getSession();

        if (comfirm != null) {
            if (comfirm.equalsIgnoreCase("yes")) {
                String csvpath = (String) session.getAttribute("csvpath");
                if (csvpath != null) {
                    try {
                        List<Student> studentsList = new ArrayList();
                        File f = new File(csvpath);
                        if (f.exists()) {
                            RandomAccessFile raf = new RandomAccessFile(csvpath, "r");
                            String s = "";
                            raf.readLine();
                            StringTokenizer st;
                            while ((s = raf.readLine()) != null) {
                                st = new StringTokenizer(s, ",");
                                while (st.hasMoreTokens()) {
                                    String name = st.nextToken().trim();
                                    int age = Integer.parseInt(st.nextToken().trim());
                                    int sex = Integer.parseInt(st.nextToken().trim());
                                    String hometown = st.nextToken().trim();
                                    Student person = new Student(name, age, sex, hometown);
                                    studentsList.add(person);
                                }
                            }
                            
                            if(!studentsList.isEmpty()) {
                                DAO_Student daoaddstu = new DAO_Student();
                                for(Student aStudent : studentsList) {
                                    daoaddstu.addStudent(aStudent);
                                }
                                daoaddstu.closeConnection();
                                response.sendRedirect("Student.jsp");
                            }
                        } else {
                            out.print("This file is not exist!");
                        }
                    } catch (IOException | NumberFormatException e) {
                        out.print("Some things went wrong!");
                    }
                }
            } else if (comfirm.equalsIgnoreCase("no")) {
                session.setAttribute("csvpath", null);
                response.sendRedirect("Student.jsp");
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
